package com.deyuanyun.pic.ctrl.controller;

import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.ctrl.controller.vo.CpProtectionParamExpandVO;
import com.deyuanyun.pic.ctrl.controller.vo.CpSegmentExpandVO;
import com.deyuanyun.pic.ctrl.controller.vo.CpSegmentPlanVO;
import com.deyuanyun.pic.ctrl.service.OperationPlanService;
import com.deyuanyun.pic.settings.controller.BaseController;
import com.deyuanyun.pic.settings.support.cache.SessionCacheSupport;
import com.google.common.base.Objects;
import org.joda.time.LocalDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.concurrent.locks.ReentrantLock;

/**
 *  阴保运行管理---运行计划controller
 *  @author axiao(xj08.dream@gmail.com)
 *  @date 2016-09-06
 *  @version 1.0
 *  Histroy: [修改人][时间][描述]
 */
@Controller
@RequestMapping("/corrctrl/operationPlan")
public class OperationPlanController extends BaseController {

    @Autowired
    private OperationPlanService operationPlanService;

    /**
     * 跳转到运行计划页面
     * @param
     * @return
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-09-07
     */
    @RequestMapping("/toPlanList.html")
    public String toOperPlan() {
        String roleCodes = SessionCacheSupport.getSessionVO().getRoleCodes();
        if(StringUtils.isNotEmpty(roleCodes) && roleCodes.contains("CW001")) { //为超级管理员
            return "/corrctrl/cathodic/cathodicPlan";
        } else {
            return "/corrctrl/cathodic/cathodicCalendar";
        }
    }

    /**
     * 查询线路阴保运行计划
     * @param segmentPlanVO 线路阴保计划实体类
     * @return
     * @throws
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-09-08
     */
    @RequestMapping("/querySegmentPlan.json")
    @ResponseBody
    public void querySegmentPlan(CpSegmentPlanVO segmentPlanVO) {
        PageJsonBean jsonBean = new PageJsonBean();
        jsonBean.setList(operationPlanService.querySegmentPlan(segmentPlanVO));
        AjaxSupport.sendSuccessText("success", jsonBean);
    }


    /**
     * 删除线路阴保计划列表
     * @param segmentId 线路id
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-09-12
     */
    @RequestMapping("/deletePlan.json")
    @ResponseBody
    public void deleteSegmentPlan(String segmentId) {
        operationPlanService.deleteSegmentPlan(segmentId);
        AjaxSupport.sendSuccessText("success", "删除成功");
    }

    /**
     * 新建或者详情线路阴保运行计划
     * @param id 在详情跳转的时候需要有值
     * @return
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-09-08
     */
    @RequestMapping("/createSegmentPlan.html")
    public String createSegmentPlan(String id, ModelMap modelMap) {
        if(StringUtils.isNotEmpty(id)) {
            CpSegmentPlanVO cpSegment  = new CpSegmentPlanVO();
            cpSegment.setId(id);
            List<CpSegmentPlanVO> segments = operationPlanService.querySegmentPlan(cpSegment);
            if(segments != null && !segments.isEmpty()) {
                modelMap.addAttribute("cpSegment", segments.get(0));
            }
        }
        return "/corrctrl/cathodic/cathodicDetails";
    }

    /**
     * 查询阴保运行标准计划
     * @param segment 阴保设施运行线路
     * @return
     * @throws
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-09-08
     */
    @RequestMapping("/queryPlan.json")
    @ResponseBody
    public void queryPlan(CpSegmentExpandVO segment) {
        AjaxSupport.sendSuccessText("success",operationPlanService.queryCpSegmentByCondition(segment));
    }

    /**
     * 判断管线设置运行计划的时候，起点终点是否和以前设置的数据存在交叉重合，如存在给予准确提示
     * @param pipeLineId 管道id
     * @param fromPointMileage 起点位置绝对距离
     * @param toPointMileage 终点位置绝对距离
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-09-21
     */
    @RequestMapping("/checkCrossOverlap.json")
    @ResponseBody
    public void checkCrossOverlap(String pipeLineId, Long fromPointMileage, Long toPointMileage) {
        Asserts.assertTrue(StringUtils.isNotEmpty(pipeLineId), "管道id不能为空！");
        Asserts.assertTrue(fromPointMileage != null, "起点位置绝对距离不能为空！");
        Asserts.assertTrue(toPointMileage != null, "终点位置绝对距离不能为空！");
        operationPlanService.queryCrossOverlap(pipeLineId, fromPointMileage, toPointMileage);
        AjaxSupport.sendSuccessText("success", "");
    }

    private ReentrantLock lock = new ReentrantLock();

    /**
     * 保存阴保运行计划
     * @param segment 阴保设施运行线路
     * @return
     * @throws
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-09-08
     */
    @RequestMapping("/saveOrReleasePlan.json")
    @ResponseBody
    public void saveOrReleasePlan(CpSegmentExpandVO segment) {
        try {
            lock.lock();
            String dataStated =  segment.getDataStated();
            boolean isRelease = Objects.equal("20", dataStated);
            operationPlanService.saveOrReleasePlan(segment, isRelease);
            AjaxSupport.sendSuccessText("success", (isRelease ? "发布" : "保存") + "成功");
        } finally {
            lock.unlock();
        }
    }

    /**
     * 保存单项阴保运行计划
     * @param segment 阴保设施运行线路
     * @return
     * @throws
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-09-08
     */
    @RequestMapping("/saveSinglePlan.json")
    @ResponseBody
    public void saveSinglePlan(CpSegmentExpandVO segment) {
        try {
            lock.lock();
            AjaxSupport.sendSuccessText("success", operationPlanService.savePlan(segment));
        } finally {
            lock.unlock();
        }
    }

    /**
     * 获取单条记录的检测周期文本
     * @param paramExpandVO
     * @return
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-09-21
     */
    @RequestMapping("/getSaveProParamResult.json")
    @ResponseBody
    public void getSaveProParamResult(CpProtectionParamExpandVO paramExpandVO) {
        AjaxSupport.sendSuccessText("success", paramExpandVO);
    }

    /**
     * 获取阴保计划
     * @param dateStr 时间只要年月
     * @param pipeLineId 管道id
     * @param fromId 起点位置id
     * @param toId 终点位置id
     * @param showValveRoom 是否显示场站阀室日历
     * @return 
     * @author axiao(xj08.dream@gmail.com)
     * @date 2016-09-12
     */
    @RequestMapping("/ybCalendarPlan.json")
    @ResponseBody
    public void ybCalendarPlan(String dateStr, String pipeLineId, String fromId, String toId, boolean showValveRoom) {
        Asserts.assertTrue(StringUtils.isNotEmpty(dateStr), "日期范围不能为空！");
        AjaxSupport.sendSuccessText("success", operationPlanService.taskCalendar(LocalDate.parse(dateStr).toDate(), pipeLineId, fromId, toId, showValveRoom));
    }

}
